package com.holike.crm.fragment.report.multiple;

import android.os.Bundle;
import android.os.Handler;

import androidx.annotation.NonNull;

import com.holike.crm.presenter.GeneralReportPresenter;

/**
 * Created by pony on 2019/11/7.
 * Version v3.0 app报表
 * 空间业绩分析
 */
public class SpaceAnalysisFragment extends AbsAnalysisFragment<SpaceAnalysisHelper> implements GeneralReportPresenter.OnGetEvaluateTypeCallback2 {
    private Handler mHandler;

    public static SpaceAnalysisFragment newInstance(String subTitle, String dimensionOf) {
        Bundle bundle = new Bundle();
        bundle.putBoolean("entry", true);
        bundle.putString("subTitle", subTitle);
        bundle.putString("dimensionOf", dimensionOf);
        SpaceAnalysisFragment fragment = new SpaceAnalysisFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @NonNull
    @Override
    protected SpaceAnalysisHelper newHelper() {
        return new SpaceAnalysisHelper(this, this);
    }

    @Override
    public void requestArea(String type, String cityCode) {
        mPresenter.getFirstAreaName(type, cityCode, this);
    }

    @Override
    public void onGetAreaNameSuccess(String msg) {
        mHelper.setAreaName(msg);
    }

    @Override
    public void onGetAreaNameFailure(String failReason) {
        if (mHandler == null) {
            mHandler = new Handler();
        }
        mHandler.postDelayed(() -> mHelper.requestArea(), 500);
    }

    @Override
    public void onSelectArea() {
        removeCallback();
    }

    @Override
    public void onDestroyView() {
        removeCallback();
        super.onDestroyView();
    }

    private void removeCallback() {
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
    }
}
